<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/19
 * Time: 11:38
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;

class ItemCateController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct() {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/itemCate/ajaxGetCateList'
        ]);
        parent::__construct();
    }

    /**
     * 分类列表
     */
    public function cateList(){
        $status = I('get.status');
        $name = I('get.name');

        //分页
        $page = I('get.page', 1);
        $pagesize = 20;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $status && $params['status'] = $status;
        $name && $params['name'] = $name;
        $params['depth'] = 1;
        $result = D('Resource/ItemCate', 'Service')->searchItemCateList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $cateList = $result['list'];

        //生成分页
        $url = "/ItemCate/cateList?status={$status}&name={$name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('status', $status);
        $this->assign('name', $name);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);
        $this->assign('cateList',$cateList);

        $this->loadFrame('cateList');
    }

    /**
     *  编辑&&新增
     */
    public function editCate(){
        $id = I('get.id');
        $cateInfo = [];
        if($id){
            $cateInfo = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$id]);
        }
        $this->assign('cateInfo',$cateInfo);
        $this->loadFrame('editCate');
    }
    /**
     * 分类列表
     */
    public function cateList2(){
        $status = I('get.status');
        $name = I('get.name');

        //分页
        $page = I('get.page', 1);
        $pagesize = 20;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $status && $params['status'] = $status;
        $name && $params['name'] = $name;
        $params['depth'] = 2;
        $result = D('Resource/ItemCate', 'Service')->searchItemCateList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $cateList = $result['list'];

        //生成分页
        $url = "/ItemCate/cateList2?status={$status}&name={$name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('status', $status);
        $this->assign('name', $name);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);
        $this->assign('cateList',$cateList);

        $this->loadFrame('cateList2');
    }

    /**
     *  编辑&&新增
     */
    public function editCate2(){
        $id = I('get.id');
        $cateInfo = [];
        if($id){
            $cateInfo = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$id]);
        }
        $data = [];
        $data['status'] = 1;
        $data['depth'] = 1;
        $p_cate = D('Resource/ItemCate','Service')->getAllCateList($data);

        $this->assign('cateInfo',$cateInfo);
        $this->assign('p_cate',$p_cate);
        $this->loadFrame('editCate2');
    }
    /**
     * 分类列表
     */
    public function cateList3(){
        $status = I('get.status');
        $name = I('get.name');

        //分页
        $page = I('get.page', 1);
        $pagesize = 20;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $status && $params['status'] = $status;
        $name && $params['name'] = $name;
        $params['depth'] = 3;
        $result = D('Resource/ItemCate', 'Service')->searchItemCateList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $cateList = $result['list'];

        //生成分页
        $url = "/ItemCate/cateList3?status={$status}&name={$name}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('status', $status);
        $this->assign('name', $name);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);
        $this->assign('cateList',$cateList);

        $this->loadFrame('cateList3');
    }

    /**
     *  编辑&&新增
     */
    public function editCate3(){
        $id = I('get.id');
        $cateInfo = [];
        if($id){
            $cateInfo = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$id]);
        }
        $data = [];
        $data['status'] = 1;
        $data['depth'] = 2;
        $p_cate = D('Resource/ItemCate','Service')->getAllCateList($data);

        $this->assign('cateInfo',$cateInfo);
        $this->assign('p_cate',$p_cate);
        $this->loadFrame('editCate3');
    }
    /**
     * @return string
     */
    public function ajaxSaveCate(){
        $id = I('post.id');
        $pid = I('post.pid');
        $name = I('post.name');
        $is_schedule = I('post.is_schedule');
        $is_enquiry = I('post.is_enquiry');
        $status = I('post.status');

        $data = [];
        $id && $data['id'] = $id;
        $data['pid'] = $pid;
        $name && $data['name'] = $name;
        $status && $data['status'] = $status;
        $is_schedule && $data['is_schedule'] = $is_schedule;
        $is_enquiry && $data['is_enquiry'] = $is_enquiry;

        if(!$data){
           return Response(201,"无需操作");
        }
        D('ItemCate','Logic')->saveItemCate($data);
    }

    /**
     * @return string
     */
    public function ajaxGetCateList(){
        $pid = I('post.pid');

        if(!$pid){
            return Response(201,"参数异常");
        }

        $list = D('Resource/ItemCate','Service')->getAllCateList(['pid'=>$pid,'status'=>1]);

        if(!$list){
            return Response(202,"该分类下暂无子分类");
        }
        return Response(999,"请求成功",$list);
    }

    /**
     * @return string
     */
    public function deleteCate(){
        $id = I('post.id');
        $status = I('post.status');

        $data = [];
        $id && $data['id'] = $id;
        $status && $data['status'] = $status;

        if(!$data){
            return Response(201,"无需操作");
        }
        D('ItemCate','Logic')->deleteItemCate($data);
    }
}